package com.liang.controller;

import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.swing.JOptionPane;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liang.pojo.Gorillaceo;
import com.liang.pojo.Role;
import com.liang.service.RoleService;

@Controller
public class RoleController {
	@Autowired
	RoleService roleService;

	@RequestMapping("/role/selectAll")
	@ResponseBody
	/*
	 * public Object selectAll(@RequestParam(value = "pageNum",defaultValue =
	 * "1")int pageNum) { //这个必须放在查询条件的前面，最好第一行 PageHelper.startPage(pageNum, 12);
	 * List<Role> roleList=roleService.selectAllRole(); PageInfo<Role> pageInfo=new
	 * PageInfo<Role>(roleList); return pageInfo; }
	 * 
	 * @RequestMapping("/role/selectPageText")
	 * 
	 * @ResponseBody
	 */
	public Object selectPageText(@RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
			@RequestParam(value = "pageText", required = false) String pageText) {
		// 这个必须放在查询条件的前面，最好第一行
		PageHelper.startPage(pageNum, 12);
		Map<String, Object> map = new TreeMap<String, Object>();
		if (pageText == null || "".equals(pageText)) {
			List<Role> roleList = roleService.selectAllRole(map);
			PageInfo<Role> pageInfo = new PageInfo<Role>(roleList);
			return pageInfo;
		}
		if (pageText.contains("%")) {
			pageText = pageText.replaceAll("%", "\\\\%");
		}
		map.put("pageText", pageText);
		List<Role> roleList = roleService.selectAllRole(map);
		PageInfo<Role> pageInfo = new PageInfo<Role>(roleList);
		return pageInfo;
	}

	@RequestMapping("/role/toUpdate")
	public String toUpdate(@RequestParam("id") String id, Model model) {
		Role role = roleService.selectRoleById(Integer.parseInt(id));
		model.addAttribute("role", role);
		return "update";
	}

	@RequestMapping("/role/update")
	public String update(Role role, @RequestParam("id") String id, @RequestParam("parentId") String parentId,
			@RequestParam("locked") String locked, Model model) {
		if (Integer.parseInt(parentId) < 0) {
			model.addAttribute("msg1", "不能小于0");
			toUpdate(id, model);
			return "update";
		}
		if (Integer.parseInt(locked) > 1 || Integer.parseInt(locked) < 0) {
			model.addAttribute("msg", "只能输入0或1");
			toUpdate(id, model);
			return "update";
		}
		role.setId(Integer.parseInt(id));
		role.setParentId(Integer.parseInt(parentId));
		role.setLocked(Integer.parseInt(locked));
		roleService.updateRole(role);
		return "main";
	}

	/*
	 * @RequestMapping("/role/delete") public String
	 * delete(@RequestParam("id")String id) { int
	 * res=JOptionPane.showConfirmDialog(null, "是否继续删除", "是否继续",
	 * JOptionPane.YES_NO_OPTION); if(res==JOptionPane.YES_OPTION){
	 * roleService.deleteRoleById(Integer.parseInt(id)); //点击“是”后执行这个代码块 } else{
	 * System.out.println("选择否后执行的代码"); //点击“否”后执行这个代码块 }
	 * 
	 * return "main"; }
	 */
	@ResponseBody
	@RequestMapping("/role/deleteBatch")
	public Object deleteBatch(Gorillaceo gorillaceo) {
		int res = JOptionPane.showConfirmDialog(null, "是否继续删除", "是否继续", JOptionPane.YES_NO_OPTION);
		int i = 0;
		if (res == JOptionPane.YES_OPTION) {
			i = roleService.deleteRoleByList(gorillaceo); // 点击“是”后执行这个代码块
		} else {
			i = -1; // 点击“否”后执行这个代码块 }
		}
		return i;
	}
	
	@RequestMapping("/role/insertTwo")
	public String insertTwo() {
		Role role = new Role();
		for (int i = 0; i < 11; i++) {
//			role.setId(30+i);
			role.setRole("user:create" + i);
			role.setDescription("用户创建" + i);
			role.setParentId(0);
			role.setLocked(0);
			System.out.println(roleService.insertRole(role));
		}
		return "main";
	}

	@RequestMapping("/role/deleteTwo")
	public String deleteTwo() {
		for (int i = 500; i < 505; i++) {

			System.out.println(roleService.deleteRoleById(i));
		}
		return "main";
	}
}
